package com.zzxz.practicaltrainingsys.controller;

import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.common.Result;
import com.zzxz.practicaltrainingsys.entity.Weeklynews;
import com.zzxz.practicaltrainingsys.service.WeeklynewsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weeklynews")
@CrossOrigin
public class WeeklynewsController {
    @Autowired
    private WeeklynewsService weeklynewsService;

    //获取某个项目的周报
    @GetMapping("/project")
    public ResponseEntity<?> getWeeklyNewsByProjectId(@RequestParam Integer projectId) {
        try {
            List<Map<String, Object>> result = weeklynewsService.getWeeklyNewsByProjectId(projectId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("获取周报列表失败: " + e.getMessage());
        }
    }

    //获取指定项目和组下的组长周报
    //http://localhost:8080/weeklynews/leader-news
    @GetMapping("/leader-news")
    public Result<List<Weeklynews>> getLeaderWeeklyByGroup(
            @RequestParam("projectId") Integer projectId,
            @RequestParam("groupId") Integer groupId) {
        List<Weeklynews> newsList = weeklynewsService.getLeaderWeeklyByGroup(projectId, groupId);
        return Result.success(newsList);
    }

    //用来联合学生表的学号以及项目表的项目编号来查该项目该学生的周报
    //http://localhost:8080/weeklynews/students
    @GetMapping("/list")
    public List<Weeklynews> getWeeklynewsByStudentId(@RequestParam Integer studentId)
    {
        return weeklynewsService.getWeeklynewsByStudentId(studentId);
    }

    //周报详情
    //http://localhost:8080/weeklynews/detail?weeklynewsId=3
    @GetMapping("/detail")
    public Weeklynews getWeeklynewsById(@RequestParam Integer weeklynewsId) {
        return weeklynewsService.getWeeklynewsById(weeklynewsId);
    }

    //新增周报
    //http://localhost:8080/weeklynews/insert
    @PostMapping("/insert")
    public int insertWeeklynews(@RequestBody Weeklynews weeklynews) {
        int result = weeklynewsService.insertWeeklynews(weeklynews);
        return result;
    }

    //更新周报
    //http://localhost:8080/weeklynews/update
    @PutMapping("/update")
    public int updateWeeklynews(@RequestBody Weeklynews weeklynews) {
        return weeklynewsService.updateWeeklynews(weeklynews);
    }

    //用来查询所有周报信息
    //http://localhost:8080/weeklynews/all
    @GetMapping("/all")
    public List<Map<String, Object>> getAllWeeklyNews() {
        return weeklynewsService.getAllWeeklyNews();
    }

    @ApiOperation("查找周报信息（分页查询）")
    @GetMapping("/pages")
    public PageInfo<Map<String, Object>> getWeeklyNewsByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return weeklynewsService.getWeeklyNewsByPage(pageNum, pageSize);
    }


}
